<template>
  <div style="width: 100%">
    <v-tableTitle :btnConfig="btnConfig" @cancel="cancel" @save="save" ></v-tableTitle>
    <div class="detailBox" style="height:300px">
      <v-detail ref="detail"></v-detail>
    </div>
    <div class="detailListBox">
      <v-detailList ref="detailList" :apiParams="apiParams" @handleSave="save"></v-detailList>
    </div>
  </div>
</template>

<script>
import baseMixin from "@/mixins/BaseMixin";

import tableTitle from "commons/TableTitle";
import detail from "../details/Detail";
import detailList from "../detailsList/DetailList";
import { readdirSync } from "fs";

export default {
  name: "dispatchDetail",
  mixins: [baseMixin],
  data() {
    return {
      apiParams:{deliveryId:-1}
    };
  },
  computed: {
    tableConfig1() {
      return [
        {
          label: this.$t('in.taskdetailkey'),
          prop: "taskdetailkey",
          input: "input",
          disabled: false,
        },
        {
          label: this.$t('config.shipper'),
          prop: "storerkey",
          input: "input",
          disabled: false,
        },
        {
          label: this.$t('config.sku'),
          prop: "sku",
          input: "input",
          disabled: false,
        },
        {
          label: this.$t('in.qty'),
          prop: "qty",
          eidtFlag: true,
          verify(value, that) {
            if (value === "") {
              that.$message.error(this.$tj('in.qtyreceived','app.emptyMes'));
              return false;
            } else {
              var re = /^[0-9]*$/;
              let te = value;
              let flag = re.test(te);
              if (flag) {
                if (value < 0) {
                  that.$message.error(this.$t('app.inputGreaterZeroNumberPlease'));
                  return false;
                } else {
                  value = parseInt(value);
                  return true;
                }
              } else {
                that.$message.error(this.$t('app.inputGreaterZeroNumberPlease'));
                return false;
              }
            }
          },
        },
        {
          label: this.$t('bill.lot'),
          prop: "lot",
          input: "input",
          disabled: false,
        },
        {
          label: this.$t('config.toLoc'),
          prop: "toloc",
          eidtFlag: true,
          verify(value, that) {
            value = value.replace(/\s*/g, "");
            if (value === "") {
              that.$message.error(this.$tj('config.loc','app.emptyMes'));
              return false;
            } else {
              return true;
            }
          },
        },
        {
          label: this.$t('in.toid'),
          prop: "toid",
          disabled: false,
          input: "input",
        },
        {
          label: this.$t('common.status'),
          prop: "status",
          input: "select",
          disabled: false,
          options: this.getOptions("codelkup/TMSTATUS"),
        },
        {
          label: this.$t('outbound.fromloc'),
          prop: "fromloc",
          input: "input",
          disabled: false,
        },
        {
          label: this.$t('in.fromid'),
          prop: "fromid",
          input: "input",
          disabled: false,
        },
        {
          label: this.$t('in.priority'),
          prop: "priority",
          input: "select",
          disabled: false,
          options: this.getOptions("codelkup/TMPRIORITY"),
        },
      ];
    },
    btnConfig() {
      return [
        { label: this.$t('common.save'), certigier: !this.btnPermNameList.includes('WM010113'),  invalid: "save" },
        { label: this.$t('common.cancel'), certigier: !this.btnPermNameList.includes('WM010114'),  invalid: "cancel" },
        
      ];
    },
  },
  methods: {
    //保存按钮
    save(type) {
      const detail = this.$refs.detail.detailData;
      const detailInfo = this.$refs.detailList.detailData;
      let params = {...detail,details:[detailInfo]};
      //有id为修改，否则为新增
      if(this.$route.query?.id){
        params.id = this.$route.query.id;
        this.$post("/dcDeliveryOrder/updateDcDeliveryOrder",params).then(res => {
            this.$message.success(this.$t('app.upMes'))
            if(type == 2){
              this.$refs.detailList.activeName = 'list';
            }else{
              this.$router.go(-1)
            }
          
        })
      }else{
        this.$post("/dcDeliveryOrder/insertDcDeliveryOrder",params).then(res => {
            this.$message.success(this.$t('app.addMes'))
            if(type == 2){
              this.apiParams = {deliveryId:res.data};
              this.$refs.detailList.activeName = 'list';
            }else{
              this.$router.go(-1)
            }
          
        })
      }
    },
    //弹框取消
    cancel() {
      this.$router.go(-1)
    },
  },
  mounted(){
    if(this.$route.query?.id){
      this.apiParams = {deliveryId:this.$route.query?.id}
    }
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-detail": detail,
    "v-detailList": detailList,
  },
};
</script>

<style scoped></style>
